

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.models.absa.train.rerank_terms &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../../index.html">
          

          
            
            <img src="../../../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../../../models.html">nlp_architect.models</a> &raquo;</li>
        
      <li>nlp_architect.models.absa.train.rerank_terms</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.models.absa.train.rerank_terms</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2018 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>
<span class="kn">import</span> <span class="nn">csv</span>
<span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">tensorflow</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">PathLike</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>

<span class="kn">from</span> <span class="nn">nlp_architect.models.absa.utils</span> <span class="kn">import</span> <span class="n">_read_generic_lex_for_similarity</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.absa</span> <span class="kn">import</span> <span class="n">TRAIN_OUT</span><span class="p">,</span> <span class="n">TRAIN_LEXICONS</span><span class="p">,</span> <span class="n">GENERIC_OP_LEX</span><span class="p">,</span> <span class="n">LEXICONS_OUT</span>

<span class="kn">from</span> <span class="nn">scipy.spatial.distance</span> <span class="kn">import</span> <span class="n">cosine</span>
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">StratifiedKFold</span>

<span class="c1"># pylint: disable=import-error</span>
<span class="kn">from</span> <span class="nn">tensorflow.keras.layers</span> <span class="kn">import</span> <span class="n">Dense</span><span class="p">,</span> <span class="n">Dropout</span>
<span class="kn">from</span> <span class="nn">tensorflow.keras.models</span> <span class="kn">import</span> <span class="n">Sequential</span><span class="p">,</span> <span class="n">load_model</span>

<span class="kn">from</span> <span class="nn">nlp_architect.utils.embedding</span> <span class="kn">import</span> <span class="n">load_word_embeddings</span>


<div class="viewcode-block" id="RerankTerms"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms">[docs]</a><span class="k">class</span> <span class="nc">RerankTerms</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">model_dir</span> <span class="o">=</span> <span class="n">TRAIN_OUT</span> <span class="o">/</span> <span class="s2">&quot;reranking_model&quot;</span>
    <span class="n">train_rerank_data_path</span> <span class="o">=</span> <span class="n">TRAIN_LEXICONS</span> <span class="o">/</span> <span class="s2">&quot;RerankTrainingData.csv&quot;</span>
    <span class="n">PREDICTION_THRESHOLD</span> <span class="o">=</span> <span class="mf">0.7</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">vector_cache</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">rerank_model</span><span class="p">:</span> <span class="n">PathLike</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">emb_model_path</span><span class="p">:</span> <span class="n">PathLike</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="p">):</span>
        <span class="c1"># model and training params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">embeddings_len</span> <span class="o">=</span> <span class="mi">300</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">activation_1</span> <span class="o">=</span> <span class="s2">&quot;relu&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">activation_2</span> <span class="o">=</span> <span class="s2">&quot;relu&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">activation_3</span> <span class="o">=</span> <span class="s2">&quot;sigmoid&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss</span> <span class="o">=</span> <span class="s2">&quot;binary_crossentropy&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span> <span class="o">=</span> <span class="s2">&quot;rmsprop&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">epochs_and_batch_size</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seeds</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">threshold</span> <span class="o">=</span> <span class="mf">0.5</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">sim_lexicon</span> <span class="o">=</span> <span class="n">TRAIN_LEXICONS</span> <span class="o">/</span> <span class="s2">&quot;RerankSentSimLex.csv&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generic_lexicon</span> <span class="o">=</span> <span class="n">GENERIC_OP_LEX</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">vector_cache</span> <span class="o">=</span> <span class="n">vector_cache</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">word_vectors_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vectors_sim_dict</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">rerank_model_path</span> <span class="o">=</span> <span class="n">rerank_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">emb_model_path</span> <span class="o">=</span> <span class="n">emb_model_path</span>

        <span class="n">LEXICONS_OUT</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="n">tensorflow</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">set_verbosity</span><span class="p">(</span><span class="n">tensorflow</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">ERROR</span><span class="p">)</span>

<div class="viewcode-block" id="RerankTerms.calc_cosine_similarity"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.calc_cosine_similarity">[docs]</a>    <span class="k">def</span> <span class="nf">calc_cosine_similarity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">word_1</span><span class="p">,</span> <span class="n">word_2</span><span class="p">,</span> <span class="n">embedding_dict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        calculate cosine similarity scores between 2 terms</span>

<span class="sd">        Args:</span>
<span class="sd">            word_1 (str): 1st input word</span>
<span class="sd">            word_2 (str): 2nd input word</span>
<span class="sd">            embedding_dict (dict): embedding dictionary</span>

<span class="sd">        Returns:</span>
<span class="sd">            vectors_sim_dict[key] (float): similarity scores between the 2 input words</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">sorted</span><span class="p">([</span><span class="n">word_1</span><span class="p">,</span> <span class="n">word_2</span><span class="p">]))</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">vector_cache</span> <span class="ow">or</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">vectors_sim_dict</span><span class="p">:</span>
            <span class="n">vector_1</span> <span class="o">=</span> <span class="n">embedding_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">word_1</span><span class="p">)</span>
            <span class="n">vector_2</span> <span class="o">=</span> <span class="n">embedding_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">word_2</span><span class="p">)</span>

            <span class="c1"># check if both words have vectors</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">count_nonzero</span><span class="p">(</span><span class="n">vector_1</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">np</span><span class="o">.</span><span class="n">count_nonzero</span><span class="p">(</span><span class="n">vector_2</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sim_score</span> <span class="o">=</span> <span class="n">cosine</span><span class="p">(</span><span class="n">vector_1</span><span class="p">,</span> <span class="n">vector_2</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sim_score</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">vectors_sim_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">sim_score</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">vectors_sim_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span></div>

<div class="viewcode-block" id="RerankTerms.calc_similarity_scores_for_all_terms"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.calc_similarity_scores_for_all_terms">[docs]</a>    <span class="k">def</span> <span class="nf">calc_similarity_scores_for_all_terms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">generic_terms</span><span class="p">,</span> <span class="n">embedding_dict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        calculate similarity scores between each term and each off the generic terms</span>

<span class="sd">        Args:</span>
<span class="sd">            terms: candidate terms</span>
<span class="sd">            generic_terms: generic opinion terms</span>
<span class="sd">            embedding_dict: embedding dictionary</span>

<span class="sd">        Returns:</span>
<span class="sd">            neg_all: similarity scores between each cand term and neg generic term</span>
<span class="sd">            pos_all: similarity scores between each cand term and pos generic term</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Computing similarity scores...</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="n">neg_all</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">pos_all</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">term</span> <span class="ow">in</span> <span class="n">terms</span><span class="p">:</span>
            <span class="n">polarity_sim_dic</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;NEG&quot;</span><span class="p">:</span> <span class="p">[],</span> <span class="s2">&quot;POS&quot;</span><span class="p">:</span> <span class="p">[]}</span>
            <span class="k">for</span> <span class="n">generic_term</span><span class="p">,</span> <span class="n">polarity</span> <span class="ow">in</span> <span class="n">generic_terms</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>

                <span class="n">sim_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_cosine_similarity</span><span class="p">(</span><span class="n">term</span><span class="p">,</span> <span class="n">generic_term</span><span class="p">,</span> <span class="n">embedding_dict</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">sim_score</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">polarity_sim_dic</span><span class="p">[</span><span class="n">polarity</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sim_score</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">polarity_sim_dic</span><span class="p">[</span><span class="n">polarity</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>

            <span class="n">neg_all</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">polarity_sim_dic</span><span class="p">[</span><span class="s2">&quot;NEG&quot;</span><span class="p">])</span>
            <span class="n">pos_all</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">polarity_sim_dic</span><span class="p">[</span><span class="s2">&quot;POS&quot;</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">neg_all</span><span class="p">,</span> <span class="n">pos_all</span></div>

<div class="viewcode-block" id="RerankTerms.load_terms_and_polarities"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.load_terms_and_polarities">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">load_terms_and_polarities</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        load terms and polarities from file</span>

<span class="sd">        Args:</span>
<span class="sd">            filename: feature table file full path</span>

<span class="sd">        Returns:</span>
<span class="sd">            terms: candidate terms</span>
<span class="sd">            polarities: opinion polarity per term</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Loading training data from </span><span class="si">{}</span><span class="s2"> ...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>

        <span class="n">table</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">,</span> <span class="n">skip_header</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">table</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Error: Term file is empty, no terms to re-rank.&quot;</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">terms</span> <span class="o">=</span> <span class="n">table</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n\n</span><span class="s2">Error converting str to float in training table: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>

        <span class="n">polarities</span> <span class="o">=</span> <span class="n">table</span><span class="p">[:,</span> <span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">terms</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">polarities</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Count of opinion terms is different than the count of loaded polarities.&quot;</span>
            <span class="p">)</span>
        <span class="n">polarities</span> <span class="o">=</span> <span class="p">{</span><span class="n">terms</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="n">polarities</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">terms</span><span class="p">))}</span>

        <span class="nb">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">terms</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot; features loaded from CSV file&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span></div>

<div class="viewcode-block" id="RerankTerms.load_terms_and_y_labels"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.load_terms_and_y_labels">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">load_terms_and_y_labels</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Load terms and Y labels from feature file.</span>

<span class="sd">        Args:</span>
<span class="sd">            filename: feature table file full path</span>

<span class="sd">        Returns:</span>
<span class="sd">            x: feature vector</span>
<span class="sd">            y: labels vector</span>
<span class="sd">            terms: candidate terms</span>
<span class="sd">            polarities: opinion polarity per term</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Loading basic features from </span><span class="si">{}</span><span class="s2"> ...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>

        <span class="n">table</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">,</span> <span class="n">skip_header</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">table</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Error: Terms file is empty, no terms to re-rank.&quot;</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">terms</span> <span class="o">=</span> <span class="n">table</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n\n</span><span class="s2">Error converting str to float in training table: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>

        <span class="n">y</span> <span class="o">=</span> <span class="n">table</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
        <span class="n">polarities</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="nb">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">terms</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot; features loaded from CSV file&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">y</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span></div>

<div class="viewcode-block" id="RerankTerms.concat_sim_scores_and_features"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.concat_sim_scores_and_features">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">concat_sim_scores_and_features</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">neg_sim</span><span class="p">,</span> <span class="n">pos_sim</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        concatenate similarity scores to features</span>

<span class="sd">        Args:</span>
<span class="sd">            x: feature vector</span>
<span class="sd">            neg_sim: similarity scores between cand terms and neg opinion terms</span>
<span class="sd">            pos_sim: similarity scores between cand terms and pos opinion terms</span>

<span class="sd">        Returns:</span>
<span class="sd">            x: concatenated features and similarity scores</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">neg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">neg_sim</span><span class="p">)</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pos_sim</span><span class="p">)</span>

        <span class="n">neg_avg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">neg</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">neg_std</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">neg</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">neg_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">neg</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">neg_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">neg</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="n">pos_avg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">pos_std</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">pos_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">pos_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Adding polarity similarity features...&quot;</span><span class="p">)</span>

        <span class="n">res_x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span>
            <span class="p">(</span><span class="n">neg_avg</span><span class="p">,</span> <span class="n">neg_std</span><span class="p">,</span> <span class="n">neg_min</span><span class="p">,</span> <span class="n">neg_max</span><span class="p">,</span> <span class="n">pos_avg</span><span class="p">,</span> <span class="n">pos_std</span><span class="p">,</span> <span class="n">pos_min</span><span class="p">,</span> <span class="n">pos_max</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="mi">1</span>
        <span class="p">)</span>

        <span class="k">return</span> <span class="n">res_x</span></div>

<div class="viewcode-block" id="RerankTerms.generate_embbeding_features"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.generate_embbeding_features">[docs]</a>    <span class="k">def</span> <span class="nf">generate_embbeding_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">embedding_dict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        concatenate word embedding to features</span>

<span class="sd">        Args:</span>
<span class="sd">            terms: candidate terms</span>
<span class="sd">            embedding_dict: embedding dictionary</span>
<span class="sd">            word_to_emb_idx: index to embedding dictionary</span>
<span class="sd">        Returns:</span>
<span class="sd">            x: concatenated features and word embs</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Adding word vector features...</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">vec_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">terms</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">embeddings_len</span><span class="p">))</span>

        <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">term</span> <span class="ow">in</span> <span class="n">terms</span><span class="p">:</span>
            <span class="n">word_vector</span> <span class="o">=</span> <span class="n">embedding_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">term</span><span class="p">)</span>
            <span class="n">vec_matrix</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">word_vector</span>
            <span class="n">j</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="n">x</span> <span class="o">=</span> <span class="n">vec_matrix</span><span class="p">[:</span><span class="n">j</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">x</span></div>

<div class="viewcode-block" id="RerankTerms.load_terms_and_y_labels_and_generate_features"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.load_terms_and_y_labels_and_generate_features">[docs]</a>    <span class="k">def</span> <span class="nf">load_terms_and_y_labels_and_generate_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">       load candidate terms with their basic features, Y labels and polarities from feature file</span>

<span class="sd">       Args:</span>
<span class="sd">           filename: feature table file path</span>
<span class="sd">       Returns:</span>
<span class="sd">           x: feature vector</span>
<span class="sd">           y: labels vector</span>
<span class="sd">           terms: candidate terms</span>
<span class="sd">           polarities: opinion polarity per term</span>
<span class="sd">       &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Loading feature table...</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="n">y</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_terms_and_y_labels</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>

        <span class="n">x</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_features</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span><span class="p">)</span>

        <span class="n">y_vector</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">y_vector</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">y_vector</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span></div>

<div class="viewcode-block" id="RerankTerms.load_terms_and_generate_features"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.load_terms_and_generate_features">[docs]</a>    <span class="k">def</span> <span class="nf">load_terms_and_generate_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">       load candidate terms with their basic features, Y labels and polarities from feature file</span>

<span class="sd">       Args:</span>
<span class="sd">           filename: feature table file path</span>
<span class="sd">       Returns:</span>
<span class="sd">           x: feature vector</span>
<span class="sd">           terms: candidate terms</span>
<span class="sd">           polarities: opinion polarity per term</span>
<span class="sd">       &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Loading feature table...</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_terms_and_polarities</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>

        <span class="n">x</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_features</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_determine_unk_polarities</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span><span class="p">,</span> <span class="n">neg</span><span class="p">,</span> <span class="n">pos</span><span class="p">):</span>

        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">term</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">terms</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">average</span><span class="p">(</span><span class="n">pos</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="n">np</span><span class="o">.</span><span class="n">average</span><span class="p">(</span><span class="n">neg</span><span class="p">[</span><span class="n">i</span><span class="p">]):</span>
                <span class="n">polarities</span><span class="p">[</span><span class="n">term</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;POS&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">polarities</span><span class="p">[</span><span class="n">term</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;NEG&quot;</span>

        <span class="k">return</span> <span class="n">polarities</span>

<div class="viewcode-block" id="RerankTerms.generate_features"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.generate_features">[docs]</a>    <span class="k">def</span> <span class="nf">generate_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span><span class="p">):</span>

        <span class="n">generic_terms</span> <span class="o">=</span> <span class="n">_read_generic_lex_for_similarity</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generic_lexicon</span><span class="p">)</span>
        <span class="c1"># generate unified list of candidate terms and generic terms</span>
        <span class="n">terms_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">term</span> <span class="k">for</span> <span class="n">term</span> <span class="ow">in</span> <span class="n">terms</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">term</span> <span class="ow">in</span> <span class="n">generic_terms</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">terms_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">term</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s2">&quot;&#39;</span><span class="se">\&quot;</span><span class="s2">&quot;</span><span class="p">))</span>

        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Loading embedding model...</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">embedding_dict</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">load_word_embeddings</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">emb_model_path</span><span class="p">,</span> <span class="n">terms_list</span><span class="p">)</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_embbeding_features</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">embedding_dict</span><span class="p">)</span>
        <span class="n">neg</span><span class="p">,</span> <span class="n">pos</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_similarity_scores_for_all_terms</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">generic_terms</span><span class="p">,</span> <span class="n">embedding_dict</span><span class="p">)</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">concat_sim_scores_and_features</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">neg</span><span class="p">,</span> <span class="n">pos</span><span class="p">)</span>
        <span class="n">polarities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_determine_unk_polarities</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span><span class="p">,</span> <span class="n">neg</span><span class="p">,</span> <span class="n">pos</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Dimensions of X: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span></div>

<div class="viewcode-block" id="RerankTerms.evaluate"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.evaluate">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">x_test</span><span class="p">,</span> <span class="n">y_test</span><span class="p">,</span> <span class="n">terms</span><span class="p">):</span>
        <span class="n">report</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_test</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">tp</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">fp</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">tn</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">fn</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">prediction</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">predictions</span><span class="p">):</span>
            <span class="n">y_true</span> <span class="o">=</span> <span class="n">y_test</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">prediction</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">threshold</span><span class="p">:</span>
                <span class="n">y_pred</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">y_pred</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="n">report</span><span class="p">[</span><span class="n">terms</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="p">(</span><span class="n">prediction</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">y_true</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">y_pred</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">y_true</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">tp</span> <span class="o">=</span> <span class="n">tp</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">fp</span> <span class="o">=</span> <span class="n">fp</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="n">y_true</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">tn</span> <span class="o">=</span> <span class="n">tn</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">fn</span> <span class="o">=</span> <span class="n">fn</span> <span class="o">+</span> <span class="mi">1</span>

        <span class="n">prec</span> <span class="o">=</span> <span class="mi">100</span> <span class="o">*</span> <span class="n">tp</span> <span class="o">/</span> <span class="p">(</span><span class="n">tp</span> <span class="o">+</span> <span class="n">fp</span><span class="p">)</span>
        <span class="n">rec</span> <span class="o">=</span> <span class="mi">100</span> <span class="o">*</span> <span class="n">tp</span> <span class="o">/</span> <span class="p">(</span><span class="n">tp</span> <span class="o">+</span> <span class="n">fn</span><span class="p">)</span>
        <span class="n">f1</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">prec</span> <span class="o">*</span> <span class="n">rec</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">prec</span> <span class="o">+</span> <span class="n">rec</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">(</span><span class="n">prec</span><span class="p">,</span> <span class="n">rec</span><span class="p">,</span> <span class="n">f1</span><span class="p">),</span> <span class="n">report</span></div>

<div class="viewcode-block" id="RerankTerms.generate_model"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.generate_model">[docs]</a>    <span class="k">def</span> <span class="nf">generate_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_vector_dimension</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate MLP model.</span>

<span class="sd">        Args:</span>
<span class="sd">           input_vector_dimension (int): word emb vec length</span>

<span class="sd">        Returns:</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mlp_model</span> <span class="o">=</span> <span class="n">Sequential</span><span class="p">()</span>

        <span class="n">mlp_model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dense</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">activation_1</span><span class="p">,</span> <span class="n">input_dim</span><span class="o">=</span><span class="n">input_vector_dimension</span><span class="p">))</span>
        <span class="n">mlp_model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dropout</span><span class="p">(</span><span class="mf">0.5</span><span class="p">))</span>
        <span class="n">mlp_model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dense</span><span class="p">(</span><span class="mi">64</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">activation_2</span><span class="p">))</span>
        <span class="n">mlp_model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dropout</span><span class="p">(</span><span class="mf">0.5</span><span class="p">))</span>
        <span class="n">mlp_model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dense</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">activation_3</span><span class="p">))</span>
        <span class="n">mlp_model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;accuracy&quot;</span><span class="p">],</span> <span class="n">loss</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">loss</span><span class="p">,</span> <span class="n">optimizer</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">mlp_model</span></div>

<div class="viewcode-block" id="RerankTerms.predict"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_table_file</span><span class="p">,</span> <span class="n">generic_opinion_terms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Predict classification class according to model.</span>

<span class="sd">        Args:</span>
<span class="sd">           input_table_file: feature(X) and labels(Y) table file</span>
<span class="sd">           generic_opinion_terms: generic opinion terms file name</span>

<span class="sd">        Returns:</span>
<span class="sd">            final_concat_opinion_lex: reranked_lex conctenated with generic lex</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">x</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">polarities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_terms_and_generate_features</span><span class="p">(</span><span class="n">input_table_file</span><span class="p">)</span>

        <span class="n">model</span> <span class="o">=</span> <span class="n">load_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rerank_model_path</span><span class="p">)</span>
        <span class="n">reranked_lexicon</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

        <span class="n">reranked_lex</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">prediction</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">reranked_lexicon</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">prediction</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="ow">and</span> <span class="n">prediction</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">PREDICTION_THRESHOLD</span><span class="p">:</span>
                <span class="n">reranked_lex</span><span class="p">[</span><span class="n">terms</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="p">(</span><span class="n">prediction</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">polarities</span><span class="p">[</span><span class="n">terms</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>

        <span class="n">final_concat_opinion_lex</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_concat_reranked_lex</span><span class="p">(</span>
            <span class="n">reranked_lex</span><span class="p">,</span> <span class="n">generic_opinion_terms</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">final_concat_opinion_lex</span></div>

<div class="viewcode-block" id="RerankTerms.rerank_train"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.rerank_train">[docs]</a>    <span class="k">def</span> <span class="nf">rerank_train</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Class for training a reranking model.&quot;&quot;&quot;</span>
        <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_terms_and_y_labels_and_generate_features</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">train_rerank_data_path</span>
        <span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Model training...&quot;</span><span class="p">)</span>
            <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_model</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">e</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs_and_batch_size</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">b</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs_and_batch_size</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>

            <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="n">e</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">b</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">RerankTerms</span><span class="o">.</span><span class="n">model_dir</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

            <span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">RerankTerms</span><span class="o">.</span><span class="n">model_dir</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;/rerank_model.h5&quot;</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Saved model to: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">RerankTerms</span><span class="o">.</span><span class="n">model_dir</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;/rerank_model.h5&quot;</span><span class="p">)</span>

        <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Division by zero, skipping test&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="RerankTerms.cross_validation_training"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.cross_validation_training">[docs]</a>    <span class="k">def</span> <span class="nf">cross_validation_training</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Perform k fold cross validation and evaluate the results.&quot;&quot;&quot;</span>
        <span class="n">final_report</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">y_vector</span><span class="p">,</span> <span class="n">terms</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_terms_and_y_labels_and_generate_features</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">train_rerank_data_path</span>
        <span class="p">)</span>

        <span class="k">for</span> <span class="n">seed</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">seeds</span><span class="p">:</span>
            <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">epochs</span><span class="p">,</span> <span class="n">batch_size</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs_and_batch_size</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">print_params</span><span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">epochs</span><span class="p">,</span> <span class="n">seed</span><span class="p">)</span>
                <span class="n">k_fold</span> <span class="o">=</span> <span class="n">StratifiedKFold</span><span class="p">(</span><span class="n">n_splits</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="n">seed</span><span class="p">)</span>
                <span class="n">f1_scores</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">precision_scores</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">recall_scores</span> <span class="o">=</span> <span class="p">[]</span>

                <span class="k">try</span><span class="p">:</span>

                    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">k_fold</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)):</span>
                        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_model</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                        <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span>
                            <span class="n">x</span><span class="p">[</span><span class="n">train</span><span class="p">],</span>
                            <span class="n">y_vector</span><span class="p">[</span><span class="n">train</span><span class="p">],</span>
                            <span class="n">epochs</span><span class="o">=</span><span class="n">epochs</span><span class="p">,</span>
                            <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span>
                            <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                        <span class="p">)</span>

                        <span class="n">measures</span><span class="p">,</span> <span class="n">report</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span>
                            <span class="n">model</span><span class="p">,</span> <span class="n">x</span><span class="p">[</span><span class="n">test</span><span class="p">],</span> <span class="n">y_vector</span><span class="p">[</span><span class="n">test</span><span class="p">],</span> <span class="n">terms</span><span class="p">[</span><span class="n">test</span><span class="p">]</span>
                        <span class="p">)</span>
                        <span class="n">final_report</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">report</span><span class="p">)</span>

                        <span class="n">precision</span><span class="p">,</span> <span class="n">recall</span><span class="p">,</span> <span class="n">f1</span> <span class="o">=</span> <span class="n">measures</span>
                        <span class="n">f1_scores</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f1</span><span class="p">)</span>
                        <span class="n">precision_scores</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">precision</span><span class="p">)</span>
                        <span class="n">recall_scores</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">recall</span><span class="p">)</span>

                        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
                            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Fold &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</span><span class="p">)</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">print_evaluation_results</span><span class="p">(</span><span class="n">precision</span><span class="p">,</span> <span class="n">recall</span><span class="p">,</span> <span class="n">f1</span><span class="p">)</span>

                    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Summary:&quot;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">print_evaluation_results</span><span class="p">(</span><span class="n">precision_scores</span><span class="p">,</span> <span class="n">recall_scores</span><span class="p">,</span> <span class="n">f1_scores</span><span class="p">)</span>

                <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
                    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Division by zero, skipping test&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">write_evaluation_report</span><span class="p">(</span><span class="n">final_report</span><span class="p">)</span></div>

<div class="viewcode-block" id="RerankTerms.print_params"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.print_params">[docs]</a>    <span class="k">def</span> <span class="nf">print_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">epochs</span><span class="p">,</span> <span class="n">seed</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print training params.</span>

<span class="sd">        Args:</span>
<span class="sd">            batch_size(int): batch size</span>
<span class="sd">            epochs(int): num of epochs</span>
<span class="sd">            seed(int): seed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span>
            <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Model Parameters: act_1= &quot;</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation_1</span>
            <span class="o">+</span> <span class="s2">&quot;, act_2= &quot;</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation_2</span>
            <span class="o">+</span> <span class="s2">&quot;, act_3= &quot;</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation_3</span>
            <span class="o">+</span> <span class="s2">&quot;, loss= &quot;</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss</span>
            <span class="o">+</span> <span class="s2">&quot;, optimizer= &quot;</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span>
            <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">seed= &quot;</span>
            <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
            <span class="o">+</span> <span class="s2">&quot;, epochs= &quot;</span>
            <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">epochs</span><span class="p">)</span>
            <span class="o">+</span> <span class="s2">&quot;, batch_size= &quot;</span>
            <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">batch_size</span><span class="p">)</span>
            <span class="o">+</span> <span class="s2">&quot;, threshold= &quot;</span>
            <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">threshold</span><span class="p">)</span>
            <span class="o">+</span> <span class="s2">&quot;, use_complete_w2v= &quot;</span>
            <span class="o">+</span> <span class="s2">&quot;, sim_lexicon= &quot;</span>
            <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sim_lexicon</span><span class="p">)</span>
            <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="RerankTerms.print_evaluation_results"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.print_evaluation_results">[docs]</a>    <span class="k">def</span> <span class="nf">print_evaluation_results</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">precision</span><span class="p">,</span> <span class="n">recall</span><span class="p">,</span> <span class="n">f1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print evaluation results.</span>

<span class="sd">        Args:</span>
<span class="sd">            precision(list of float): precision</span>
<span class="sd">            recall(list of float): recall</span>
<span class="sd">            f1(list of float): f measure</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">print_measure</span><span class="p">(</span><span class="s2">&quot;Precision&quot;</span><span class="p">,</span> <span class="n">precision</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">print_measure</span><span class="p">(</span><span class="s2">&quot;Recall&quot;</span><span class="p">,</span> <span class="n">recall</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">print_measure</span><span class="p">(</span><span class="s2">&quot;F-measure&quot;</span><span class="p">,</span> <span class="n">f1</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span>
            <span class="s2">&quot;-------------------------------------------------------------------------&quot;</span>
            <span class="s2">&quot;------------------------------&quot;</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="RerankTerms.print_measure"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.print_measure">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">print_measure</span><span class="p">(</span><span class="n">measure</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print single measure.</span>

<span class="sd">        Args:</span>
<span class="sd">            measure(str): measure type</span>
<span class="sd">            value(list of float): value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">measure</span> <span class="o">+</span> <span class="s2">&quot;: </span><span class="si">{:.2f}</span><span class="s2">%&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">value</span><span class="p">)),</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isscalar</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot; (+/- </span><span class="si">{:.2f}</span><span class="s2">%)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">value</span><span class="p">)),</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">()</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_generate_concat_reranked_lex</span><span class="p">(</span><span class="n">acquired_opinion_lex</span><span class="p">,</span> <span class="n">generic_opinion_lex_file</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Loading generic sentiment terms from </span><span class="si">{}</span><span class="s2">...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">generic_opinion_lex_file</span><span class="p">))</span>
        <span class="n">generics_table</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span>
            <span class="n">generic_opinion_lex_file</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">,</span> <span class="n">skip_header</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">str</span>
        <span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">generics_table</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot; generic sentiment terms loaded&quot;</span><span class="p">)</span>

        <span class="n">concat_opinion_dict</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">acquired_opinion_lex</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">concat_opinion_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;Y&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">generics_table</span><span class="p">:</span>
            <span class="n">concat_opinion_dict</span><span class="p">[</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;N&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">concat_opinion_dict</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_write_prediction_results</span><span class="p">(</span><span class="n">concat_opinion_dict</span><span class="p">,</span> <span class="n">out_override</span><span class="p">):</span>
        <span class="n">out_dir</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">out_override</span><span class="p">)</span> <span class="k">if</span> <span class="n">out_override</span> <span class="k">else</span> <span class="n">LEXICONS_OUT</span>
        <span class="n">out_path</span> <span class="o">=</span> <span class="n">out_dir</span> <span class="o">/</span> <span class="s2">&quot;generated_opinion_lex_reranked.csv&quot;</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">out_path</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">csv_file</span><span class="p">:</span>
            <span class="n">writer</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">writer</span><span class="p">(</span><span class="n">csv_file</span><span class="p">)</span>
            <span class="n">writer</span><span class="o">.</span><span class="n">writerow</span><span class="p">([</span><span class="s2">&quot;Term&quot;</span><span class="p">,</span> <span class="s2">&quot;Score&quot;</span><span class="p">,</span> <span class="s2">&quot;Polarity&quot;</span><span class="p">,</span> <span class="s2">&quot;isAcquired&quot;</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">concat_opinion_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">writer</span><span class="o">.</span><span class="n">writerow</span><span class="p">([</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Reranked opinion lexicon written to </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">out_path</span><span class="p">))</span>

<div class="viewcode-block" id="RerankTerms.write_evaluation_report"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.write_evaluation_report">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">write_evaluation_report</span><span class="p">(</span><span class="n">report_dic</span><span class="p">):</span>
        <span class="n">RerankTerms</span><span class="o">.</span><span class="n">model_dir</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">out_path</span> <span class="o">=</span> <span class="n">RerankTerms</span><span class="o">.</span><span class="n">model_dir</span> <span class="o">/</span> <span class="s2">&quot;rerank_classifier_results.csv&quot;</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">out_path</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">csv_file</span><span class="p">:</span>
            <span class="n">writer</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">writer</span><span class="p">(</span><span class="n">csv_file</span><span class="p">)</span>
            <span class="n">writer</span><span class="o">.</span><span class="n">writerow</span><span class="p">([</span><span class="s2">&quot;term&quot;</span><span class="p">,</span> <span class="s2">&quot;score&quot;</span><span class="p">,</span> <span class="s2">&quot;y_pred&quot;</span><span class="p">,</span> <span class="s2">&quot;y_true&quot;</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">report_dic</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">writer</span><span class="o">.</span><span class="n">writerow</span><span class="p">([</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Report written to </span><span class="si">{}</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">out_path</span><span class="p">))</span></div>

<div class="viewcode-block" id="RerankTerms.load_word_vectors_dict"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.train.html#nlp_architect.models.absa.train.rerank_terms.RerankTerms.load_word_vectors_dict">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">load_word_vectors_dict</span><span class="p">():</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">RerankTerms</span><span class="o">.</span><span class="n">model_dir</span> <span class="o">/</span> <span class="s2">&quot;word_vectors_dict.pickle&quot;</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">return</span> <span class="n">ret</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>